<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="member_task_cycleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" align="center" prop="id" />
      <el-table-column label="任务标识" align="center" prop="tag" />
      <el-table-column label="描述" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pagination.total > 0"
      :total="pagination.total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="handleQuery"
    />
    <!-- 添加或修改任务标识对话框 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务标识" prop="tag">
          <el-input v-model="form.tag" placeholder="请输入任务标识" />
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input v-model="form.remark" type="textarea" rows="5" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addMember_task_tag,
  getMember_task_tag,
  delMember_task_tag,
  listMember_task_tag,
  updateMember_task_tag
} from '@/api/ums/member_task_tag'

export default {
  name: 'MemberTaskCycle',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      // 任务标识表格数据
      member_task_cycleList: [],
      // 弹出层
      dialog: {
        title: undefined,
        visible: false,
        type: undefined // type 操作类型：1-新增 2-修改
      },
      // 查询参数
      queryParams: {
        tag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tag: [
          { required: true, message: '任务标识不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询任务标识列表 */
    getList() {
      this.loading = true
      Object.keys(this.queryParams).forEach(key => {
        this.queryParams[key] === '' ? this.queryParams[key] = null : false
      })
      this.queryParams.pageNum = this.pagination.pageNum
      this.queryParams.pageSize = this.pagination.pageSize
      listMember_task_tag(this.queryParams).then(response => {
        const { data } = response
        this.member_task_cycleList = data.list
        this.pagination.total = data.totalCount
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.reset()
      this.closeDialog()
    },
    handleReset() {
      this.pagination = {
        pageNum: 1,
        pageSize: 10,
        total: 0
      }
      this.queryParams = {
        tag: null
      }
      this.handleQuery()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tag: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    resetForm(val) {
      if (this.$refs[val]) {
        this.$refs[val].resetFields()
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.dialog = {
        title: '添加任务标识',
        visible: true,
        type: 'add'
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.dialog = {
        title: '修改任务标识',
        visible: true,
        type: 'edit'
      }
      const id = row.id || this.ids
      getMember_task_tag(id).then(response => {
        this.form = response.data
        // this.open = true;
        // this.title = "修改任务标识";
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.dialog.type === 'edit') {
            updateMember_task_tag(this.form).then(response => {
              this.$message.success('修改成功')
              this.closeDialog()
              this.getList()
            })
          } else {
            addMember_task_tag(this.form).then(response => {
              this.$message.success('新增成功')
              this.closeDialog()
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids
      this.$confirm('是否确认删除任务标识编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delMember_task_tag(ids)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      })
    },
    /** 导出按钮操作 */
    // handleExport () {
    //   const queryParams = this.queryParams
    //   delete queryParams.pageNum
    //   delete queryParams.pageSize
    //   this.$confirm('是否确认导出所有任务标识数据项?', '警告', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(function () {
    //     return exportMember_task_cycle(queryParams)
    //   }).then(response => {
    //     downloadFile(response.data, 'client', 'xls')
    //   })
    // },
    // handleSizeChange(val) {
    //     this.queryParams.pageSize = val
    //     this.getList()
    // },
    // handleCurrentChange(val) {
    //     this.queryParams.pageNum = val
    //     this.tableList = []
    //     this.getList()
    // },
    closeDialog() {
      this.dialog = {
        title: undefined,
        visible: false,
        type: undefined
      }
    }
  }
}
</script>
